<template>
  <div class="p-2" style="height: 100%; display: flex; flex-direction: column;">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="园区名称" prop="parkName">
              <el-input v-model="queryParams.parkName" placeholder="请输入园区名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="园区编号" prop="parkInfoCode">
              <el-input v-model="queryParams.parkInfoCode" placeholder="请输入园区编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never" style="flex: 1; display: flex; flex-direction: column; position: relative;">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['park:park-info:add']" type="primary" plain icon="Plus" @click="openAdd">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['park:park-info:edit']" type="success" plain icon="Edit" :disabled="single" @click="openUpdate()">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['park:park-info:remove']" type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['park:park-info:export']" type="warning" plain icon="Download" @click="handleExport">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="parkInfoList" style="flex: 1;" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="true" label="主键" align="center" prop="parkInfoId" />
        <el-table-column label="备注" align="center" prop="remark" min-width="120" show-overflow-tooltip />
        <el-table-column label="园区名称" align="center" prop="parkName" min-width="120" show-overflow-tooltip />
        <el-table-column label="园区编号" align="center" prop="parkInfoCode" min-width="120" show-overflow-tooltip />
        <el-table-column label="操作" align="center" fixed="right">
          <template #default="scope">
            <el-button v-hasPermi="['park:park-info:edit']" link type="primary" @click="openUpdate(scope.row)">修改</el-button>
            <el-button v-hasPermi="['park:park-info:remove']" link type="primary" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>

    <!-- 添加园区信息对话框 -->
    <!-- eslint-disable prettier/prettier -->
    <el-drawer v-model="showAdd" :title="'添加园区信息'" size="500px" append-to-body destroy-on-close>
        <Add @close="showAdd = false" @success="getList()" />
    </el-drawer>

    <!-- 修改园区信息对话框 -->
    <!-- eslint-disable prettier/prettier -->
    <el-drawer v-model="showUpdate" :title="'修改园区信息'" size="500px" append-to-body destroy-on-close>
        <Update :park-info-id="selectedParkInfoId" @close="showUpdate = false" @success="getList()" />
    </el-drawer>
  </div>
</template>

<!-- eslint-disable prettier/prettier -->
<script setup name="ParkInfo" lang="ts">
import Update from './components/Update.vue'
import Add from './components/Add.vue'
import request from '@/utils/request';

const { proxy } = getCurrentInstance() as any;

const parkInfoList = ref<any[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const selectedParkInfoId = ref();

const queryFormRef = ref<ElFormInstance>();

const queryParams = ref<any>({
  pageNum: 1,
  pageSize: 10,
  parkName: undefined,
  parkInfoCode: undefined,
  params: {
  }
})

/** 查询园区信息列表 */
const getList = async () => {
  loading.value = true;

  const res = await request({ url: '/park/park-info/list', method: 'get', params: queryParams.value })
  parkInfoList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: any[]) => {
  ids.value = selection.map(item => item.parkInfoId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const showAdd = ref(false)
const openAdd = () => {
  showAdd.value = true;
}

/** 修改按钮操作 */
const showUpdate = ref(false)
const openUpdate = async (row?: any) => {
  selectedParkInfoId.value = row?.parkInfoId || ids.value[0]

  showUpdate.value = true;
}

/** 删除按钮操作 */
const handleDelete = async (row?: any) => {
  const _parkInfoIds = row?.parkInfoId || ids.value;
  await proxy?.$modal.confirm('是否确认删除园区信息编号为"' + _parkInfoIds + '"的数据项？').finally(() => {});

  proxy?.$modal.loading('正在处理...')
  await request({ url: '/park/park-info/' + _parkInfoIds, method: 'delete' }).finally(() => proxy?.$modal.closeLoading())
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('park/park-info/export', {
    ...queryParams.value
  }, `park-info_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
<style lang="scss" scoped>
    :deep(.el-card__header) {
        height: 60px;
    }
    :deep(.el-card__body) {
        height: calc(100% - 60px);
        display: flex;
        flex-direction: column;
    }
    :deep(.pagination-container) {
        margin-top: 0;
    }
</style>
